package com.mooral.web.action.voucher;

import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import com.mooral.bean.Voucher;
import com.mooral.context.Context;
import com.mooral.dataAccess.DataAccessConnection;
import com.mooral.web.action.MooralAction;

public class RedirectAction extends MooralAction {

 	public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, Context context) throws Exception {

		  DynaActionForm dform = (DynaActionForm) form;
		  String slug = (String)dform.get("slug");
		  
		  DataAccessConnection dataAccessConnection = context.getDataAccessConnection();

		  String sql = "select 				v.id as id," +
			  		"						v.title as title," +
			  		"						v.image as image," +
			  		"						v.type as type," +
			  		"						v.description as description," +
			  		"						v.slug as slug," +
			  		"						v.url as url" +
			  		"  			from 		vouchers v" +
			  		"			where 		v.slug = '" + slug + "'";
		  
		  ResultSet resultSet = dataAccessConnection.getResultSetQuery(sql);
		  
		  Voucher voucher = new Voucher();
		  if(resultSet.next()) voucher.populate(resultSet);
		  
		  dataAccessConnection.disposeConnection();
		  
		  request.setAttribute("url", voucher.get("url"));
		  
		  return mapping.findForward(ACTION_SUCCESS);
 	}

}